<?

class Author extends Controller {

	function __construct() {

		parent::Controller();
		$data['title'] = "";

	}

	public function logout() {
		$this->load->model('Author_model');
		$this->Author_model->author_logout();
		redirect('wiki');
	}

	public function login($wiki_word = null) {
		$this->load->model('Author_model');
		$this->load->helper('form');
		$this->load->library('validation');
		$this->validation->set_rules($this->Author_model->get_login_validation_rules());
		$this->validation->set_fields($this->Author_model->get_login_validation_fields());
		$data['error'] = "";
		$data['title'] = "login";
		$this->load->model('Wiki_model');
		$data['style_css'] = $this->Wiki_model->get_current_style();
		$data['wiki_style'] = false;
		if (!is_null($wiki_word)) {
			$data['wiki_word'] = $wiki_word;
		} else {
			$data['wiki_word'] = "";
		}
		if ($this->validation->run() == FALSE) {
			$this->load->view('login',$data);
		} else {
			if ($author = $this->Author_model->check_valid_login()) {
				if (is_null($wiki_word)) {
					redirect('wiki/view/created:'.$author);
				} else {
					redirect('wiki/view/'.$wiki_word);
				}
			} else {
				$data['error'] = "invalid login information.";
				$this->load->view('login',$data);
			}
		}

	}

	public function create() {
		
		$this->load->model('Author_model');
		$this->load->helper('form');
		$this->load->library('validation');
		$this->validation->set_rules($this->Author_model->get_create_validation_rules());
		$this->validation->set_fields($this->Author_model->get_create_validation_fields());
		$data['error'] = "";
		$data['title'] = "create new account";
		$this->load->model('Wiki_model');
		$data['style_css'] = $this->Wiki_model->get_current_style();
		$data['wiki_style'] = false;

		if ($this->validation->run() == FALSE) {
			$this->load->view('author',$data);
		} else {
			if ($author_id = $this->Author_model->insert_author()) {
				//take the values from the post and go right into the login function
				$this->login();
			} else {
				$data['error'] = "There was an unspecified error inserting into the database";
				$this->load->view('author',$data);
			}
		}
	}

	public function change_password() {
		$this->load->library('authenticate');
		if (!$user = $this->authenticate->is_authenticated()) {
			redirect('author/login');
		}
		$this->load->model('Author_model');
		$this->load->helper('form');
		$this->load->library('validation');
		$this->validation->set_rules($this->Author_model->get_change_password_rules());
		$this->validation->set_fields($this->Author_model->get_change_password_fields());
		$data['error'] = "";
		$data['title'] = "change password";
		$this->load->model('Wiki_model');
		$data['style_css'] = $this->Wiki_model->get_current_style();
		$data['wiki_style'] = false;
		if ($this->validation->run() == FALSE) {
			$this->load->view('change_password',$data);
		} else {
			if (!$this->Author_model->change_password($user)) {
				$data['error'] = "Your old password was in correct";
				$this->load->view('change_password',$data);
			} else {
				redirect('wiki/view/created:'.$user['username']);
			}
		}


	}

	public function test_logged_status() {
		$this->load->library('authenticate');
		if ($user = $this->authenticate->is_authenticated()) {
			echo "logged in";
		} else {
			echo "not logged in";
		}
	}

}

?>
